import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;





public class Test {
    public static int next(int num){
        int ret = 0;
        while(num != 0){
            int tmp = num % 10;
            num /= 10;
            ret += tmp * tmp;
        }
        return ret;
    }

    public static boolean isHappy(int n) {
        int slow = n;
        int fast = (next(n));
        while(slow != fast){
            System.out.println(slow);
            if(slow == 1)return true;
            slow = next(slow);
            fast = next(next(fast));
        }
        if(slow == 1)return true;//判断相遇时是不是为1
        fast = n;
        while(fast != slow){
            //System.out.println(slow);
            slow = next(slow);
            fast = next(fast);
            if(slow == 1)return true;
        }

        return false;
    }
    public static void main(String[] args) {
        System.out.println(isHappy(2));
    }
}